

class ScheduleController extends BaseController{
    def beforeInterceptor = [action:this.auth]
    
    def index = { redirect(action:list,params:params) }

    // the delete, save and update actions only accept POST requests
    static allowedMethods = [delete:'POST', save:'POST', update:'POST']

    def list = {
        params.max = Math.min( params.max ? params.max.toInteger() : 10,  100)
        [ scheduleInstanceList: Schedule.list( params ), scheduleInstanceTotal: Schedule.count() ]
    }

    def show = {
        def scheduleInstance = Schedule.get( params.id )

        if(!scheduleInstance) {
            flash.message = "Schedule not found with id ${params.id}"
            redirect(action:list)
        }
        else { return [ scheduleInstance : scheduleInstance ] }
    }

    def delete = {
        def scheduleInstance = Schedule.get( params.id )
        if(scheduleInstance) {
            try {
                scheduleInstance.delete(flush:true)
                flash.message = "Schedule ${params.id} deleted"
                redirect(controller:'course',action:show, id:scheduleInstance.course.id)
            }
            catch(org.springframework.dao.DataIntegrityViolationException e) {
                flash.message = "Schedule ${params.id} could not be deleted"
                redirect(action:show,id:params.id)
            }
        }
        else {
            flash.message = "Schedule not found with id ${params.id}"
            redirect(action:list)
        }
    }

    def edit = {
        def scheduleInstance = Schedule.get( params.id )

        if(!scheduleInstance) {
            flash.message = "Schedule not found with id ${params.id}"
            redirect(action:list)
        }
        else {
            return [ scheduleInstance : scheduleInstance ]
        }
    }

    def update = {
        def scheduleInstance = Schedule.get( params.id )
        if(scheduleInstance) {
            if(params.version) {
                def version = params.version.toLong()
                if(scheduleInstance.version > version) {
                    
                    scheduleInstance.errors.rejectValue("version", "schedule.optimistic.locking.failure", "Another user has updated this Schedule while you were editing.")
                    render(view:'edit',model:[scheduleInstance:scheduleInstance])
                    return
                }
            }
            scheduleInstance.properties = params
            if(!scheduleInstance.hasErrors() && scheduleInstance.save()) {
                flash.message = "Schedule ${params.id} updated"
                redirect(controller:'course',action:show, id:scheduleInstance?.course?.id)
            }
            else {
                render(view:'edit',model:[scheduleInstance:scheduleInstance])
            }
        }
        else {
            flash.message = "Schedule not found with id ${params.id}"
            redirect(action:list)
        }
    }

    def create = {
        def scheduleInstance = new Schedule();
        scheduleInstance.properties = params
        return ['scheduleInstance':scheduleInstance,'courseid':params.courseid]
    }

    def save = {
        def scheduleInstance = new Schedule(params)
        Course c = Course.get(scheduleInstance?.course?.id);
        if(!scheduleInstance.hasErrors() && scheduleInstance.constraintsOk() && scheduleInstance.save()) {
            c.addToSchedules(scheduleInstance);
            c.save(flush:true);
            flash.message = "Schedule created";
            redirect(controller:"course",action:show,id:scheduleInstance.course.id)
        }
        else {
            render(view:'create',model:[scheduleInstance:scheduleInstance, 'courseid': scheduleInstance.course.id])
        }
    }

    def delete_schedule = {
        def courseId = params.courseid
        def scheduleList = Schedule.findAllByCourse(Course.get(courseId))
        render(view:'delete_schedule',model:[ scheduleList: scheduleList, courseid: courseId ])
    }

    def delete_participant = {
        def scheduleInstance = Schedule.get(params.id)
        def participantList = scheduleInstance.allParticipants()
        render(view:'delete_participant',model:[ participantList: participantList, scheduleid: params.id])
    }

    def save_deleted_schedule = {
        String[] del = request.getParameterValues("scheduleid")
        def courseInstance = Course.get(params.courseid)
        for(String x : del){
            def sched = Schedule.get(x)
            courseInstance.delSchedule(sched)
        }
        flash.message = "Schedules deleted for the course"
        redirect(controller:"course",action:show,id:params.courseid)
    }

    def save_deleted_participant = {
        String[] del = request.getParameterValues("participantid")
        def scheduleInstance = Schedule.get(params.scheduleid)
        for(String x : del){
            def participant = Employee.get(x)
            def list = scheduleInstance.participations
            for(Participations p in list){
                if(p?.participant==participant){
                    participant.removeFromParticipations(p)
                    scheduleInstance.removeFromParticipations(p)
                    p.delete()
                }
            }
        }
        flash.message = "Participants deleted for the schedule"
        redirect(controller:"schedule",action:show,id:params.scheduleid)
    }

    def list_participant = {
        def scheduleInstance = Schedule.get(params.id)
        return ['scheduleInstance':scheduleInstance]
    }
}
